package com.liu.db;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.liu.crypto.Aes;
import com.liu.keeppwd.SysState;
import com.liu.model.Group;
import com.liu.model.GroupComparator;
import com.liu.myerror.KeyEmptyError;

public class DbGroup {
	static String[] zd = { "name", "jyUUID", "status" };
	static int[] type = { 1, 1, 2 };

	public static String getDbCreateS() {
		String res = "create table acgroup(";
		res += "UUID char(32) primary key,";

		for (int i = 0; i < zd.length; i++) {
			res += zd[i];
			if (type[i] == 1) {
				res += " varchar(2000)";
			} else {
				res += " bigint";
			}
			if (i != zd.length - 1) {
				res += ",";
			}
		}
		res += ")";
		return res;
	}

	public static List<Object[]> AddRawData(Group ac) {
		List<Object[]> res = new ArrayList<Object[]>();
		String res1 = "delete from acgroup where UUID=?";
		String res2 = "insert into acgroup(UUID,name,jyUUID,status) values(?,?,?,?)";
		Object[] obj1 = new Object[2];
		Object[] obj2 = new Object[5];
		obj1[0] = ac.getUUID();
		obj1[1] = res1;
		obj2[0] = ac.getUUID();
		obj2[1] = ac.getName();
		obj2[2] = ac.getJyUUID();
		obj2[3] = ac.getStatus();
		obj2[4] = res2;
		res.add(obj1);
		res.add(obj2);
		return res;
	}

	public static List<Object[]> Add(Group ac,String key) throws Exception {
		if (key == null || key.equals("")) {
			throw new KeyEmptyError();
		}
		String res = "insert into acgroup(UUID,name,jyUUID,status) values(?,?,?,1)";
		Object[] obj = new Object[4];
		obj[0] = ac.getUUID();
		obj[1] = Aes.Encrypt(key, ac.getName());
		obj[2] = java.util.UUID.randomUUID().toString().replace("-", "");
		obj[3] = res;
		List<Object[]> resLst = new ArrayList<Object[]>();
		resLst.add(obj);
		Object[] obj2 = new Object[3];
		obj2[0] = "1";
		obj2[1] = "syncflag";
		obj2[2] = "update quickSave set pvalue=? where pkey=?";
		resLst.add(obj2);
		return resLst;
	}

	public static List<Object[]> Update(Group ac,String key) throws Exception {
		if (key == null || key.equals("")) {
			throw new KeyEmptyError();
		}
		List<Object> objs = new ArrayList<Object>();
		String res = "update acgroup set ";
		if (ac.getName() != null) {
			res += "name=?,";
			objs.add(Aes.Encrypt(key, ac.getName()));
		}
		if (ac.getStatus() != -99) {
			res += "status=?,";
			objs.add(ac.getStatus());
		}
		res += "jyUUID=jyUUID||'|"
				+ java.util.UUID.randomUUID().toString().replace("-", "") + "'";

		res += " where UUID=?";
		objs.add(ac.getUUID());
		objs.add(res);
		List<Object[]> resLst = new ArrayList<Object[]>();
		resLst.add(objs.toArray());
		Object[] obj2 = new Object[3];
		obj2[0] = "1";
		obj2[1] = "syncflag";
		obj2[2] = "update quickSave set pvalue=? where pkey=?";
		resLst.add(obj2);
		return resLst;
	}

	@SuppressWarnings("unchecked")
	public static List<Group> SelectWithDecrypt(SQLiteDatabase dbms, int status,String key)
			throws Exception {
		if (key == null || key.equals("")) {
			throw new KeyEmptyError();
		}
		List<Group> res = Select(dbms, status);
		for (int i = 0; i < res.size(); i++) {
			Group a = res.get(i);
			a.setName(Aes.Decrypt(key, a.getName()));
		}
		Collections.sort(res, new GroupComparator());
		return res;

	}

	public static List<Group> Select(SQLiteDatabase dbms,int status) {
		List<Group> res = new ArrayList<Group>();
		String selStr = "select UUID,name,jyUUID,status from acgroup";
		if(status==1){
			selStr+=" where status=1";
		}
		Cursor cursor = dbms.rawQuery(selStr, new String[] {});
		while (cursor.moveToNext()) {
			Group ac = new Group();
			ac.setUUID(cursor.getString(0));
			ac.setName(cursor.getString(1));
			ac.setJyUUID(cursor.getString(2));
			ac.setStatus(cursor.getInt(3));
			res.add(ac);
		}
		return res;
	}

	public static Group getWithDecrypt(SQLiteDatabase dbms, String uuid,String key)
			throws Exception {
		if (key == null || key.equals("")) {
			throw new KeyEmptyError();
		}
		Group res = get(dbms, uuid);
		if (res == null)
			return null;
		res.setName(Aes.Decrypt(key, res.getName()));
		return res;
	}

	public static Group get(SQLiteDatabase dbms, String uuid) {
		String selStr = "select UUID,name,jyUUID,status from acgroup where UUID=?";
		Cursor cursor = dbms.rawQuery(selStr, new String[] { uuid });
		if (cursor.moveToNext()) {
			Group ac = new Group();
			ac.setUUID(cursor.getString(0));
			ac.setName(cursor.getString(1));
			ac.setJyUUID(cursor.getString(2));
			ac.setStatus(cursor.getInt(3));
			return ac;
		}
		return null;
	}

	public static Object[] CleanData() {
		List<Object> objs = new ArrayList<Object>();
		String res = "delete from acgroup ";
		objs.add(res);
		return objs.toArray();
	}

}
